<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组的拓展方法</title>
</head>
<body>
  <input type="text" placeholder="搜索课程" id="J_input">
  <ul class="J_list">
    <span>暂无数据</span>
  </ul>
    <script src="./js/utils.js"></script>
    <script type="text/javascript">
    

      var data = [
      {
        "name": "张三",
        "age": "22",
        "email": "13076682658@163.com"
      },
      {
        "name": "李四",
        "age": "21",
        "email": "13076682658@163.com"
      },
      {
        "name": "王五",
        "age": "25",
        "email": "13076682658@163.com"
      },
      {
        "name": "小明",
        "age": "23",
        "email": "13076682658@163.com"
      },
      {
        "name": "小红",
        "age": "20",
        "email": "13076682658@163.com"
      },
    ];
      data.forEach(function(elem, index, arr){
        if(elem.age >= 22){
          elem.gender = "男";
        }else{
          elem.gender = "女";
        }
      })

    var newarr = data.filter(function(elem,index,arr){
      return elem.gender === "男";
    })

    
     
    /**
     * every
     * 1、如果有一个不满足条件就停止遍历，条件就是return后面表达式
     * 2、返回一个bool值
     * 
    */
  //  console.log(data)
   var res1 = data.every(function(elem, index, array){
    // console.log(elem); 
    return elem.gender == "女"
   })

   /**
    * some
    * 1、有一个满足条件就停止遍历，条件就是return后面表达式
    * 2、返回一个bool值
   */
   var res2 = data.some(function(elem, index, array){
    //  console.log(elem);
     return elem.gender === "女"
   })


   Array.prototype.myevery = function(fn){
    var arr = this,
        len = this.length,
        arg2 = arguments[1] || window,
        res = true;

    for(var i = 0; i < len; i++){
      if(!(fn.apply(arg2, [arr[i], i, arr]))){
        res = false;
        break;
      }
    }

    return res;
   }

   Array.prototype.mysome = function(fn){
    var arr = this,
        len = this.length,
        arg2 = arguments[1] || window,
        res = false;
    
    for(var i = 0; i < len; i++){
      if(fn.apply(arg2, [arr[i], i, arr])){
        res = true;
        break;
      }
    }
    return res;
   }

   var myres1 = data.myevery(function(elem, index, array){
     console.log(elem);
     return elem.gender === "女";
   })
   console.log(myres1);


   var myres2 = data.mysome(function(elem, index, array){
    console.log(elem); 
    return elem.gender === "女";
   })
   console.log(myres2);


   /**
    * reduce 归纳
    * 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行)，
    * 将其结果汇总为单个返回值。
   */
   var initaValue = []
   var narr = data.reduce(function(prev, elem, index, arr){
    prev.push(elem.name);    
    return prev;
   }, initaValue);
   console.log(narr, initaValue);

   var narrRig = data.reduceRight(function(prev, elem, index, arr){
    prev.push(elem.name);
    return prev
   }, initaValue);
   console.log(narrRig);

   var newdata = 'ASLDKJFL=9SD8FJLSKERJGLFLASKD; POST=JFLASD789FSD; ASLDKBJFL=9SCD8FJLSKERJGLFLASKD; ASLKDJFL=9SD8FJLSKERJGLFLASKD;ASLDKGJFL=9SD8FJLSKERJGLFLASKD; ASLDKFFL=9SD8FJLSKERJGLFLASKD;ASLFDKJFL=9SDB8FJLSKERJGLFLASKD; ASLDDJFL=9SD8FJLSKERJGLFLASKD;ASLDEKJFL=9SD8FJCLSKERJGLFLASKD;'  
   var val = newdata.split('; ');


  /**
   * 重写reduce
   * */  
  Array.prototype.myreduce = function(fn, initaValue){
    var arr = this,
        len = this.length,
        arg2 = arguments[2] || window,
        item;
    
    for(var i = 0; i < len; i++){
      initaValue = fn.apply(arg2, [initaValue, arr[i], i, arr])
    }
    return initaValue;
  }

  /**
   * 重写reduceRight
   * */ 
  Array.prototype.myreduceRight = function(fn, initaValue){
    var arr = this,
        len = this.length - 1,
        arg2 = arguments[2] || window;
    
    for(var i = len; i >= 0; i--){
      initaValue = fn.apply(arg2, [initaValue, arr[i], i, arr])
    }
    return initaValue;
  }
   
   var newval = val.myreduce(function(prev, elem){
    var item = elem.split('=');
    prev[item[0]] = item[1];
    return prev;
   }, {})
   console.log(newval);


  ;(function(doc){
    var oSearchInput = doc.getElementById('J_input'),
        oList = doc.getElementsByClassName('J_list')[0];
    
    var init = function(){
      bindEvent();
    }

    function bindEvent(){
      oSearchInput.addEventListener('input', searchCourse, false)
    }

    function searchCourse(){
      var val = this.value,
          len = val.length;
      
      if(len > 0){
        if(makList(searchData(data, val))){
          oList.innerHTML = makList(searchData(data, val));
        }else{
          oList.innerHTML = "<li> -暂无数据- </li>";
        }
      }else{
        oList.innerHTML = "<li> -暂无数据- </li>";
      }
    }

    function searchData(data, keyword){
      return data.reduce(function(prev, elem){
        var res = elem.name.indexOf(keyword);

        if(res !== -1){
          prev.push(elem);
        }
        
        return prev;
      }, []);
    }

    function makList(data){
      var list = '';
      data.forEach(function(elem){
        list += "<li>" + "名字: " + elem.name + "</li>"
      })

      return list;
    }

    init();

  })(document);
  </script>
</body>
</html>